////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011-2012 www.iflytek.com. All Rights Reserved.
//  This software for customer relationship management system, developed by Iflytek-ZY-GCZX team.
//  Software code and design for the team, copy rights reserved.
////////////////////////////////////////////////////////////////////////////////

package com.iflytek.ci.cums.util;

import java.util.List;

import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.iflytek.ci.common.utils.excel.FormatContentParser;
import com.iflytek.ci.cums.domain.Account;
import com.iflytek.ci.cums.service.impl.EncryptionPassword;
import com.iflytek.ci.main.dto.Macro;

/**
 * <p>
 * Excel导入时，Excel解析的实现类.
 * </p>
 *
 * @author poplar.yfyang
 * @version 1.0 2012-11-18 1:57 PM
 * @since JDK 1.5
 */
public class AccountExcelParser implements FormatContentParser {

	/** 所有的Excel人员信息 */
	private List<Account> account_list = Lists.newArrayList();
	/** 每行的人员信息 */
	private Account line_account;

	@Override
	public <T> Object getResult() {
		return account_list;
	}

	@Override
	public void newLine(int lineNumber) {
		line_account = new Account();
		//设置默认密码
		line_account.setPassword(EncryptionPassword.encryption(Macro.DEFAULT_PASSWORD));
		account_list.add(line_account);
	}

	@Override
	public void parseUnit(int x, int y, String content) {
		//第1列为登录名称，第2列表示人的名称，第4列表示用户名密码，第3列为邮箱
		//是否为空
		boolean content_empty = Strings.isNullOrEmpty(content);
		if (!content_empty) {
			content = content.trim();
		}
		switch (x) {
			case 0:
				line_account.setLogin(content);
				break;
			case 1:
				line_account.setName(content);
				break;
			case 2:
				line_account.setMail(content_empty ? line_account.getLogin() + "@iflytek.com" : content);
				break;
			case 3:
				if (!content_empty) {
					line_account.setPassword(EncryptionPassword.encryption(Macro.DEFAULT_PASSWORD));
				}
				break;
			default:
				break;
		}
	}
}
